package com.fe.service.impl;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fe.mapper.TeacherMapper;
import com.fe.model.Teacher;
import com.fe.service.TeacherService;
@Service
public class TeacherServiceImpl implements TeacherService {
	
	@Autowired
	private TeacherMapper teacherMapper;
	
	@Override
	public int delete(String teacher_id) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int add(Teacher teacher) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	/**
	 * 根据id查询一个家教的信息
	 * 方法描述：
	 * @param tea_id
	 * @return
	 * @author ssh
	 * @date 2018年7月30日 下午4:29:02
	 */
	@Override
	public void loadTeacher(Map<String, Object> map, Integer tea_id) {
		Teacher teacher= teacherMapper.loadTeacher(tea_id);
		if (teacher == null) {
			throw new RuntimeException("没有数据！");
		}
		map.put("teacher", teacher);
	}
	
	 /**
     * 
     * 方法描述：更改一个家教的信息
     * @param map
     * @param teacher
     * @return
     * @author ssh
     * @date 2018年8月21日 下午8:55:58
     */
	@Override
	public void update(HttpServletRequest request, Map<String, Object> map, Teacher teacher) {
		int result = teacherMapper.update(teacher);
		if (result > 0) {
			map.put("message", "更新成功！");
			HttpSession session = request.getSession();
			session.setAttribute("teacher", teacherMapper.loadTeacher(teacher.getTea_id()));
		}else {
			map.put("message", "更新失败！");
		}
	}
	
	/**
	 * 
	 * 方法描述：查询家教
	 * @param map
	 * @param request
	 * @author ssh
	 * @date 2018年8月21日 下午8:56:29
	 */
	@Override
	public void listTeacher(Map<String, Object> map, HttpServletRequest request) {
		int count = teacherMapper.count(map);
		if (count == 0) {
			throw new RuntimeException("暂无数据!");
		}
		map.put("count", count);
		List<Teacher> listTeacher = teacherMapper.listTeacher(map);
		if (listTeacher == null) {
			throw new RuntimeException("暂无数据!");
		}
		map.put("listTeacher", listTeacher);
	}

	/**
	 * 
	 * 方法描述：家教修改账号时，判断账号是否已经被使用
	 * @param tea_account
	 * @author ssh
	 * @date 2018年8月22日 下午8:28:39
	 */
	@Override
	public void exictAccount(String tea_account) {
		int result = teacherMapper.exictAccount(tea_account);
		if (result > 0) {
			throw new RuntimeException("该账号已被使用!");
		}
	}

}
